package com.zhudoo.zhilian.api;

import com.zhudoo.zhilian.service.AccessLogService;
import com.zhudoo.zhilian.service.TipService;
import com.zhudoo.zhilian.util.service.TipUtil;
import com.zhudoo.zhilian.util.web.Response;
import com.zhudoo.zhilian.util.web.ResponseUtil;
import com.zhudoo.zhilian.util.web.ServiceContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * @author zhudao
 */
@RestController
@RequestMapping("/tip")
public class TipController {

    @Autowired
    private TipService tipService;
    @Autowired
    private AccessLogService accessLogService;

    @RequestMapping(value = "/addTip", method = RequestMethod.POST)
    public Response addTip(@RequestHeader String token, Integer cityId,
                           Integer tipId, Integer line, String lineName,
                           Integer station, String stationName, Integer hall,
                           Integer price, String checkInDate, String description,
                           Integer roomType, Integer tipType, String pics) {
        ServiceContext serviceContext = ServiceContext.getInstance();
        serviceContext.addParams("token", token);
        serviceContext.addParams("tipId", tipId);
        serviceContext.addParams("cityId", cityId);
        serviceContext.addParams("tipType", tipType);
        serviceContext.addParams("line", line);
        serviceContext.addParams("lineName", lineName);
        serviceContext.addParams("station", station);
        serviceContext.addParams("stationName", stationName);
        serviceContext.addParams("hall", hall);
        serviceContext.addParams("roomType", roomType);
        serviceContext.addParams("price", price);
        serviceContext.addParams("checkInDate", checkInDate);
        serviceContext.addParams("description", description);
        serviceContext.addParams("pics", pics);
        Map<String, Object> resultMap = tipService.addTip(serviceContext);
        if (tipId == null) {
            accessLogService.addAccessLog(token, AccessLogService.B_T_TIP, 0, "发布帖子");
        } else {
            accessLogService.addAccessLog(token, AccessLogService.B_T_TIP, tipId, "修改帖子");
        }
        return ResponseUtil.responseSuccess("发布成功", resultMap);
    }

    @RequestMapping(value = "/getTipList", method = RequestMethod.GET)
    public Response getTipList(@RequestHeader String token, Integer tipType,
                               Integer roomType,
                               Integer cityId, Integer line,
                               Integer station, Integer hall,
                               String price, Integer checkInDateSort,
                               Integer priceSort,
                               Integer page, Integer limit) {
        ServiceContext serviceContext = ServiceContext.getInstance();
        serviceContext.addParams("cityId", cityId);
        serviceContext.addParams("tipType", tipType);
        serviceContext.addParams("roomType", roomType);
        serviceContext.addParams("token", token);
        serviceContext.addParams("line", line);
        serviceContext.addParams("station", station);
        serviceContext.addParams("hall", hall);
        serviceContext.setPage(page);
        serviceContext.setLimit(limit);
        serviceContext.addParams(TipUtil.splitPrice(price));
        serviceContext.addParams("checkInDateSort", checkInDateSort);
        serviceContext.addParams("priceSort", priceSort);
        List<Map<String, Object>> resultList = tipService.getTipList(serviceContext);
        return ResponseUtil.responseSuccess("获取成功", resultList);
    }

    @RequestMapping(value = "/getTip", method = RequestMethod.GET)
    public Response getTip(@RequestHeader String token, Integer tipId) {
        ServiceContext serviceContext = ServiceContext.getInstance();
        serviceContext.addParams("tipId", tipId);
        serviceContext.addParams("token", token);
        Map<String, Object> resultMap = tipService.getTipId(serviceContext);
        accessLogService.addAccessLog(token, AccessLogService.B_T_TIP, tipId, "查看帖子详情");
        return ResponseUtil.responseSuccess("获取成功", resultMap);
    }

    @RequestMapping(value = "/getMyTip", method = RequestMethod.GET)
    public Response getMyTip(@RequestHeader String token, Integer page, Integer limit) {
        ServiceContext serviceContext = ServiceContext.getInstance();
        serviceContext.addParams("token", token);
        serviceContext.setPage(page);
        serviceContext.setLimit(limit);
        List<Map<String, Object>> mapList = tipService.getTipByUserId(serviceContext);
        return ResponseUtil.responseSuccess("获取成功", mapList);
    }

    @RequestMapping(value = "/getHeTip", method = RequestMethod.GET)
    public Response getHeTip(@RequestHeader String token, Integer heUserId, Integer page, Integer limit) {
        ServiceContext serviceContext = ServiceContext.getInstance();
        serviceContext.addParams("token", token);
        serviceContext.addParams("heUserId", heUserId);
        serviceContext.setPage(page);
        serviceContext.setLimit(limit);
        List<Map<String, Object>> mapList = tipService.getTipByUserId(serviceContext);
        return ResponseUtil.responseSuccess("获取成功", mapList);
    }

    @RequestMapping(value = "/cancelTip", method = RequestMethod.POST)
    public Response cancelTip(@RequestHeader String token, Integer tipId) {
        ServiceContext serviceContext = ServiceContext.getInstance();
        serviceContext.addParams("token", token);
        serviceContext.addParams("tipId", tipId);
        tipService.cancelTip(serviceContext);
        return ResponseUtil.responseSuccess("取消成功");
    }

    @RequestMapping(value = "/shareTip", method = RequestMethod.POST)
    public Response shareTip(@RequestHeader String token, Integer tipId) {
        ServiceContext serviceContext = ServiceContext.getInstance();
        serviceContext.addParams("token", token);
        serviceContext.addParams("tipId", tipId);
        tipService.shareTip(serviceContext);
        accessLogService.addAccessLog(token, AccessLogService.B_T_TIP, tipId, "分享帖子详情");
        return ResponseUtil.responseSuccess("分享成功");
    }
}
